<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_form">
        <el-form ref="form" label-width="80px">
          <!-- 登录表单-->
          <el-form-item label="账号">
            <el-input v-model="userName" placeholder="请输入账号"></el-input>
          </el-form-item>

          <el-form-item label="密码">
            <el-input v-model="password" type="password" placeholder="请输入密码" show-password></el-input>
          </el-form-item>

          <!-- 操作按钮 -->
          <el-form-item>
            <el-button type="primary" class="login_button" @click="handleLogin()">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { userLogin } from "@/api/user";

export default {
  data() {
    return {
      /**
       * 用户名
       * @type {String}
       */
      userName: "",
      /**
       * 密码
       * @type {String}
       */
      password: "",
    };
  },
  methods: {
    /**
     * 处理用户登录
     * @returns {void}
     */
    async handleLogin() {
      try {
        // 调用登录接口
        const response = await userLogin({
          userName: this.userName,
          password: this.password,
        });

        if (response.success) {
          this.$router.push("/");
          this.$message.success('登录成功');
        } else {
          this.$message.error('登录失败');
        }
      } catch (error) {
        this.$message.error(error.message || '登录失败');
      }

    },
  },
};
</script>

<style lang="scss" scoped>
@import url('@/assets/scss/error.scss');
</style>